package com.fdb.efp.nls.api.rest;

import com.fdb.basic.framework.core.bean.KeyValue;
import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.console.common.ConsoleConstant;
import com.fdb.efp.nls.service.facade.PrdCaseManageService;
import com.fdb.efp.nls.service.facade.PrdInfoService;
import com.fdb.efp.nls.service.vo.PrdCaseManageVO;
import com.fdb.efp.nls.service.vo.PrdInfoVO;
import com.fdb.efp.nls.service.vo.PrdParamVO;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/api")
public class PrdInfoController extends AbstractController {

	@Autowired
	@Qualifier("prdInfoService")
	private PrdInfoService prdInfoService;

	@Autowired
	@Qualifier("prdCaseManageService")
	private PrdCaseManageService prdCaseManageService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * 
	 * @param vo
	 * @return
	 */
	@RequestMapping(value = "/prd/infos", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<PrdInfoVO>> queryPrdInfoAll(@RequestBody PrdInfoVO vo) {
		List<PrdInfoVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		//产品信息列表不管配置与否均按照产品种类来控制数据权限
		//DataRuleType dataRuleType = DataRuleType.getDataRuleType("04");
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
			case Owner:
				outputVo = prdInfoService.queryAllOwner(vo);
				break;
			case CurrOrg:
				outputVo = prdInfoService.queryAllCurrOrg(vo);
				break;
			case CurrDownOrg:
				outputVo = prdInfoService.queryAllCurrDownOrg(vo);
				break;
			case CurrOwnerPrd:
				outputVo = prdInfoService.queryAllCurrOwnerPrd(vo);
				break;
			default:
				outputVo = prdInfoService.queryAllOwner(vo);
				break;
		}
		KeyValue userColumnMapping = new KeyValue("createUser", "userName");
		this.displayNames(outputVo, userColumnMapping, null);
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/prd/info/{prdId}", method = RequestMethod.GET)
	public @ResponseBody ResponseData<PrdInfoVO> queryByPk(@PathVariable("prdId") String prdId) {
		PrdInfoVO inVo = new PrdInfoVO();
		inVo.setPrdId(prdId);
		PrdInfoVO outputVo = prdInfoService.queryByPk(inVo);
		this.displayOrgName(outputVo, "legalOrgCode", "legalOrgCodeName");
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param prdInfo
	 * @return
	 */
	@RequestMapping(value = "/prd/info", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody PrdInfoVO inPrdInfoVo) {
		int outputVo = prdInfoService.deleteByPk(inPrdInfoVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inPrdInfoVo
	 * @return
	 */
	@RequestMapping(value = "/prd/info", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody PrdInfoVO inPrdInfoVo) {
		inPrdInfoVo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
		inPrdInfoVo.setLastUpdateUser(inPrdInfoVo.getLoginUserId());
		int outputVo = prdInfoService.updateByPk(inPrdInfoVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inPrdInfoVo
	 * @return
	 */
	@RequestMapping(value = "/prd/info", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertPrdInfo(@RequestBody PrdInfoVO inPrdInfoVo) {

		PrdInfoVO tempVo = prdInfoService.queryByPrdCode(inPrdInfoVo);
		if (Objects.nonNull(tempVo)) {
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setMessage("产品编号是：【" + inPrdInfoVo.getPrdCode() + "】的产品信息已存在！");
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}

		inPrdInfoVo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
		inPrdInfoVo.setLastUpdateUser(inPrdInfoVo.getLoginUserId());
		int outputVo = prdInfoService.insertPrdInfo(inPrdInfoVo);
		return getResponseData(outputVo);

	}

	/**
	 * 产品数据查询
	 * 
	 * @param prdCode
	 * @return
	 */
	@RequestMapping(value = "/prd/lasPrdinfo", method = RequestMethod.POST)
	public @ResponseBody ResponseData<PrdInfoVO> queryLastPrdInfo(@RequestBody String prdCode) {
		PrdInfoVO vo = new PrdInfoVO();
		vo.setPrdCode(prdCode);
		PrdInfoVO outputVo = prdInfoService.queryLastPrdInfo(vo);
		return getResponseData(outputVo);
	}

	/**
	 * 产品停用
	 * 
	 * @param inPrdInfoVo
	 * @return
	 */
	@RequestMapping(value = "/prd/stopPrdinfo", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> stopPrdInfo(@RequestBody PrdInfoVO inPrdInfoVo) {
		// 第一步，更新关联生效专案为停用
		PrdCaseManageVO prdCaseManageVo = new PrdCaseManageVO();
		prdCaseManageVo.setPrdId(inPrdInfoVo.getPrdId());
		prdCaseManageVo.setCaseStatus(ConsoleConstant.CASE_STATUS_03);
		prdCaseManageService.updateCaseStatusByPrdId(prdCaseManageVo);
		// 第二步，更新产品状态为已停用
		inPrdInfoVo.setPrdStatus(ConsoleConstant.PRD_STATUS_03);// 已停用
		int outputVo = prdInfoService.updateByPk(inPrdInfoVo);
		return getResponseData(outputVo);
	}

	/**
	 * 查询所有生效产品列表
	 * 
	 * @param inPrdInfoVo
	 * @return
	 */
	@RequestMapping(value = "/prd/queryPrdInfoSeletor", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<PrdInfoVO>> queryPrdInfoSeletor(@RequestBody PrdInfoVO inPrdInfoVo) {
		setUserInfoToVO(inPrdInfoVo);
		List<PrdInfoVO> outputVo = prdInfoService.queryPrdInfoSeletor(inPrdInfoVo);
		return getResponseData(outputVo);
	}

	/**
	 * 查询所有停用产品列表
	 * 
	 * @param inPrdInfoVo
	 * @return
	 */
	@RequestMapping(value = "/prd/queryPrdInfoStopSeletor", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<PrdInfoVO>> queryPrdInfoStopSeletor(@RequestBody PrdInfoVO inPrdInfoVo) {
		inPrdInfoVo.setPrdStatus(ConsoleConstant.PRD_STATUS_03);
		setUserInfoToVO(inPrdInfoVo);
		List<PrdInfoVO> outputVo = prdInfoService.queryPrdInfoSeletor(inPrdInfoVo);
		return getResponseData(outputVo);
	}

	/**
	 * 产品配置查询
	 */
	@RequestMapping(value = "/prd/queryPrdinfo", method = RequestMethod.GET)
	public @ResponseBody ResponseData<PrdInfoVO> queryPrdInfo(@RequestBody PrdInfoVO prdInfoVO) {
		PrdParamVO param = new PrdParamVO();
		param.setPrdId(prdInfoVO.getPrdId());
		param.setRateScheme(true);
		PrdInfoVO vo = prdInfoService.queryPrdInfo(param);
		return getResponseData(vo);
	}

	/**
	 * 查询所有生效产品列表
	 *
	 * @param inPrdInfoVo
	 * @return
	 */
	@RequestMapping(value = "/prd/queryPrdInfoSeletor/special", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<PrdInfoVO>> queryPrdInfoByPrd(@RequestBody PrdInfoVO inPrdInfoVo) {
		setUserInfoToVO(inPrdInfoVo);
		Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType("/console/api/quoat/limit/prd/infos", getUserRoles());
		inPrdInfoVo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
		List<PrdInfoVO> outputVo = prdInfoService.queryPrdInfoByPrd(inPrdInfoVo);
		return getResponseData(outputVo);
	}
}
